package com.nutstudio.feitoys;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/4/21 0021.
 */

public class MyLineView extends View {
    private int[] valueList;
    private int lineColor = Color.parseColor("#8adcf1");
    private float lineTextSize = 40;
    private String otherStr = "";

    public MyLineView(Context context) {
        super(context);
    }

    public MyLineView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyLineView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.MyLineView, defStyleAttr, 0);
        int n = a.getIndexCount();
        for (int i = 0; i < n; i++) {
            int attr = a.getIndex(i);
            switch (attr) {
                case R.styleable.MyLineView_lineColor:
                    lineColor = a.getColor(attr, Color.YELLOW);
                    break;
                case R.styleable.MyLineView_otherStr:
                    otherStr = a.getString(attr);
                    break;
                case R.styleable.MyLineView_lineTextSize:
                    lineTextSize = a.getDimension(attr, 40);
                    break;

            }

        }
        a.recycle();
    }

    public MyLineView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    public void setHData(int[] data) {
        valueList = data;
        invalidate();
    }

    public void setLineColor(int color) {
        lineColor = color;
        invalidate();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //setMeasuredDimension(measureWidth(widthMeasureSpec),measureHeight(heightMeasureSpec));
    }

    private int measureHeight(int measureSpec) {
        int result = 0;
        int mode = MeasureSpec.getMode(measureSpec);
        int size = MeasureSpec.getSize(measureSpec);

        if (mode == MeasureSpec.AT_MOST) {
            result = 360
            ;
        } else {
            result = size * 3;
        }
        return result;

    }

    private int measureWidth(int measureSpec) {
        int result = 0;
        int mode = MeasureSpec.getMode(measureSpec);
        int size = MeasureSpec.getSize(measureSpec);
        Log.d("view", mode + ">>>");
        if (mode == MeasureSpec.EXACTLY) {
            result = size;
            Log.d("view", result + "");
        } else {
            result = 75;//根据自己的需要更改
            if (mode == MeasureSpec.AT_MOST) {
                result = Math.min(result, size);
            }
        }
        return result;
    }

    private int getMaxValue() {
        List<Integer> posHList = new ArrayList<>();
        for (int i = 0; i < valueList.length; i++) {
            posHList.add(valueList[i]);
        }
        int max = 0;
        while (true) {
            if (posHList.size() > 1) {
                if (posHList.get(0) > posHList.get(1)) {
                    posHList.remove(1);
                } else {
                    posHList.remove(0);
                }
            } else {
                max = posHList.get(0);
                break;
            }
        }
        return max;
    }

    private int getMiniValue() {
        List<Integer> posHList = new ArrayList<>();
        for (int i = 0; i < valueList.length; i++) {
            posHList.add(valueList[i]);
        }
        int mini = 0;
        while (true) {
            if (posHList.size() > 1) {
                if (posHList.get(0) > posHList.get(1)) {
                    posHList.remove(0);
                } else {
                    posHList.remove(1);
                }
            } else {
                mini = posHList.get(0);
                break;
            }
        }
        return mini;
    }

    //getheight/300;
    private double partHeight() {
        Log.d("data", "test " + (getMaxValue() - getMiniValue()));
        return ((getHeight() * 1.0f - getPaddingTop() - getPaddingBottom()) / (getMaxValue() - getMiniValue()));
    }

    @Override
    protected void onDraw(Canvas canvas) {

        if (valueList!=null&&valueList.length > 4) {
            //8adcf1
            Paint pointPaint = new Paint();
            pointPaint.setColor(lineColor);
            pointPaint.setStyle(Paint.Style.FILL);
            Paint linePaint = new Paint();
            linePaint.setColor(lineColor);
            linePaint.setAntiAlias(true);
            linePaint.setStrokeWidth(8);
            Paint textP = new Paint();
            textP.setTextSize(lineTextSize);
            int baseX = getWidth() / (valueList.length * 2);
            for (int i = 0; i < valueList.length; i++) {
                if ((i + 1) < valueList.length) {
                    try {
                        canvas.drawLine(baseX, getHeight() - ((int) (partHeight() * (valueList[i] - getMiniValue()) + getPaddingBottom())), baseX + getWidth() / valueList.length, getHeight() - ((int) (partHeight() * (valueList[i+1]  - getMiniValue())) + getPaddingBottom()), linePaint);
                    } catch (IndexOutOfBoundsException ex) {
                        ex.printStackTrace();
                    }

                }
                canvas.drawCircle(baseX, getHeight() - ((int) (partHeight() * (valueList[i]  - getMiniValue())) + getPaddingBottom()), 10, pointPaint);
                canvas.drawText(valueList[i]  + otherStr, baseX - 25, getHeight() - ((int) (partHeight() * (valueList[i]  - getMiniValue())) + getPaddingBottom()) - 24, textP);
                baseX += getWidth() / valueList.length;
            }
        }

    }
}
